﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SVM;

namespace ModelPredictPro.Predict.SVM {
    public class PredictTask : BaseSVMTask, ITask {

        #region ITask

        public override void Run() {
            Parameter svm_parameter = SyncParams();

            base.OnStepChanged("Reading and scaling the train and test dataset file......");
            Problem train = Problem.Read(TrainSetFile);
            Problem test = Problem.Read(TestSetFile);
            RangeTransform range = RangeTransform.Compute(train);
            train = range.Scale(train);
            test = range.Scale(test);
            base.OnStepChanged(string.Empty);

            base.OnStepChanged("Modeling with inputed parameter......");
            Model model = Training.Train(train, svm_parameter);
            base.OnStepChanged(string.Empty);

            base.OnStepChanged("Predicting test dataset......");
            double acc = Prediction.Predict(test, base.ResultOutputFile, model, Param.UseProbability);
            base.OnStepChanged(string.Empty);
            base.OnStepChanged(string.Format("Accuracy:{0}", acc), null);

        }

        public override IEnumerable<ITask> ReadFromTxt(string taskFileName) {
            throw new NotImplementedException();
        }

        public override void SaveToTxt(string taskFileName) {
            throw new NotImplementedException();
        }

        #endregion

        public PredictTask(ISVMPredictParam param, string TrainSetFile, string TestSetFile, string ResultOutputFile):
            base(TrainSetFile,null, TestSetFile, null, ResultOutputFile){
            this.Param = param;
            Validate();
        }
        protected void Validate() {
            ValidateFileExist("TrainSetFile", TrainSetFile);
            ValidateFileExist("TestSetFile", TestSetFile);
            ValidateNewFileCanCreated("ResultOutputFile", ResultOutputFile);
        }

        public override string ToString() {
            return string.Format(@"SVM Predict Task
Kernel Type : 【{0}】\tUse Probability : 【{1}】\tUse Parallel : 【{2}】
Param C : 【{6}】\tParam Gamma : 【{7}】
Train : 【{3}】
Test  : 【{4}】
Result  : 【{5}】".Replace(@"\t", "\t"), 
Param.KernelType, Param.UseProbability, Param.ParallelModeSetting, TrainSetFile, TestSetFile, ResultOutputFile,
Param.C, Param.Gamma
    );
        }
    }
}
